﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.School;
using SIMS.Global;
using SIMS.Company;

public partial class School_Course_CreateEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.CompanyId] != null)
            {
                hidCompanyId.Value = Session[Global.CompanyId].ToString();
                lblCompany.Text = Session[Global.CompanyName].ToString();
                if (Request.Params["id"] != null)
                {
                    int _courseId = 0;
                    if (int.TryParse(Request.Params["id"].ToString(), out _courseId) && _courseId > 0)
                    {
                        Course _Course = new Course();
                        CourseDB _CourseDB = new CourseDB();
                        _Course = _CourseDB.GetCourse(_courseId, Convert.ToInt32(hidCompanyId.Value));

                        if (_Course.Id > 0)
                        {
                            if (_Course.Error.ErrorNo > 0)
                            {
                                lblMessage.Text = _Course.Error.ErrorMessage;
                            }
                            else
                            {
                                hidCourseId.Value = _courseId.ToString();
                                txtCourseName.Text = _Course.Name;
                                txtDescription.Text = _Course.Description;
                            }
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid parameter!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "No company found!";
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                Course Course = new Course();
                CourseDB _CourseDB = new CourseDB();
                int _CourseId = 0;

                if (int.TryParse(hidCourseId.Value, out _CourseId) && _CourseId > 0)
                {
                    Course = _CourseDB.GetCourse(_CourseId, Convert.ToInt32(hidCompanyId.Value));
                }
                else
                {
                    Course.Id = 0;
                    Course.CompanyId = Convert.ToInt32(hidCompanyId.Value);
                    Course.Error = new MyError();
                }

                Course.Name = txtCourseName.Text;

                if (txtDescription.Text.Trim().Length > 0)
                {
                    Course.Description = txtDescription.Text;
                }
                else
                {
                    Course.Description = string.Empty;
                }

                Course.IsActive = false;

                _CourseDB.Save(Course);

                if (Course.Error.ErrorNo > 0)
                {
                    if (Course.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = Course.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + Course.Error.ErrorLocation + ")";
                        lblMessage.Text += Course.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + Course.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx?companyid=" + hidCompanyId.Value);
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

}